
#pragma once

#include <iostream>
#include <string>
#include <sys/socket.h>
#include<cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include<strings.h>
#include "Log.hpp"
#include "Mutex.hpp"
#include "common.hpp"
#include"inetAddr.hpp"

// 基类 , 规定创建套接字的方法
// 提供一个或者是若干个 ways to create socket



namespace SocketModule
{
    using namespace LockModule;

    using namespace LogMudule;
    class Socket
    {
    public:
        virtual ~Scoket() = default;
        virtual void SetSocketOpt() = 0;
        virtual int SocketOrDie() = 0;
        virtual bool ListenOrDie() = 0;
        virtual SockPtr Acceptor() = 0;
        virtual void Close() = 0;
        virtual bool BindOrDie(int port)  = 0;
        

        //  zdl:: provide a discussion to create a 固定的 socket
        void BuildTcpSocketMethod(int port)
        {
            SocketOrDie();
            SetSocketOpt();
            BindOrDie(port); 
            ListenOrDie();
        }
    };
    using SockPtr = std::shared_ptr<Socket>;
    class TcpSocket : public Socket
    {

    public:
        virtual ~TcpSocket()
        {
        }
        virtual void SetSocketOpt() override
        {

        }
        virtual int SocketOrDie() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(LogLevel::ERROR) << "socket error";
                exit(socket_error);
            }
            LOG(LogLevel::DEBUG) << "socket success" << _sockfd;
        }
        virtual bool ListenOrDie(int listenSockfd) override
        {
            if (_sockfd == gdefaultsockfd) return false;
            int n = ::listen(_sockfd, gbacklog);
            if (n < 0)
            {
                LOG(LogLevel::ERROR) << "listen error";
                exit(listen_error);
            }
            LOG(LogLevel::DEBUG) << "listen success" << _sockfd;
            return true;
        }
        virtual SockPtr Acceptor(inetAddr* client) 
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);

            int newsockfd = ::accept(_sockfd, conv(&peer), &len);

            if (newsockfd < 0)
            {
                LOG(LogLevel::WARNING) << "accept error";

                return nullptr;
            }
            client->SetAddr(peer, len);

            return std::make_shared<TcpSocket>(newsockfd);
        }
        virtual void Close() override
        {
            if (_sockfd == gdefaultsockfd) return;
            ::close(_sockfd);
        }
        virtual bool BindOrDie(int port) override
        {
            if (_sockfd == gdefaultsockfd) return false;
            inetAddr addr(port);

            int n = ::bind(_sockfd, addr.netAdddr(), addr.netAddrLen());
            if (n < 0)
            {
                LOG(LogLevel::ERROR) << "bind error";
                exit(bind_error);
            }
            LOG(LogLevel::DEBUG) << "bind create success" << _sockfd;

            return true;
        }
        TcpSocket(int sockfd = gdefaultsockfd):_sockfd(sockfd)
        {

        }
    private:
        int _sockfd;
    };

    int main()
    {
        Socket *sk = new TcpSocket();
        sk->BuildTcpSocket(8080);
    }
}

// zld:: 现在就是银行为这个东西没有了什么能成为我的阻碍




